/*
 * $Id$
 */
package com.ptdworld.ratking;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * PrimeFaces文件复制
 */
public class PrimeFacesCopy {

    /** 用OfflineExplorer下载的PrimeFaces的保存路径 */
//    public final static String LAYOUTS_PATH = "D:\\OfflineExplorerPortable\\Download\\www.primefaces.org";
    public final static String LAYOUTS_PATH = "D:\\OfflineExplorerPortable\\Download\\www.primefaces.org\\ultima";
//    public final static String LAYOUTS_PATH = "D:\\OfflineExplorerPortable\\Download\\www.primefaces.org\\poseidon";
//    public final static String LAYOUTS_PATH = "D:\\OfflineExplorerPortable\\Download\\www.primefaces.org\\omega";

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        Path sourcePath = Paths.get(LAYOUTS_PATH);
        Path targetPath = Paths.get(LAYOUTS_PATH + "-newdir-" + (new SimpleDateFormat("yyyyMMdd")).format(new Date()));
        // 遍历文件树
        Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                //System.out.println("Debug: 原文件 =" + file);
                String fileName = file.getFileName().toString();
                //System.out.println("Debug: fileName=" + fileName);
                //System.out.println("Debug: fileName.endsWith(\".xhtml\")=" + (fileName.endsWith(".xhtml")));
                if (fileName.contains(".css.xhtml")
                        || fileName.contains(".js.xhtml")
                        || fileName.contains(".gif.xhtml")
                        || fileName.contains(".jpg.xhtml")
                        || fileName.contains(".jpeg.xhtml")
                        || fileName.contains(".png.xhtml")
                        || fileName.contains(".ttf.xhtml")
                        || fileName.contains(".eot.xhtml")
                        || fileName.contains(".woff.xhtml")
                        || fileName.contains(".woff2.xhtml")
                        || fileName.contains(".svg.xhtml")) {
                    // 新文件名(去除".xhtml"(含)之后的部分)
                    String newFileName = fileName.substring(0, fileName.indexOf(".xhtml"));
                    // 新路径
                    Path newPath = targetPath.resolve(file.subpath(sourcePath.getNameCount(), file.getNameCount() - 1));
                    if (!Files.exists(newPath)) {
                        // 创建新路径
                        Files.createDirectories(newPath);
                    }
                    Path newFile = newPath.resolve(newFileName);
                    // 复制文件
                    Files.copy(file, newFile, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println("Debug: 新文件->" + newFile);
                }
                // 继续处理其余文件
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
